package com.dcsgo.socketPlay.server;

import com.dcsgo.socketPlay.common.Message;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * @author DCSGO
 * @version 1.0
 * 为Client提供的服务
 */
public class Services {
    private static Long lastMesCnt = 0L;

    //接收消息
    public static Message receiveMessage(Socket clientSocket) throws IOException, ClassNotFoundException {
        ObjectInputStream ois = new ObjectInputStream(clientSocket.getInputStream());
        Message receiveMes = (Message) ois.readObject();
        if (receiveMes.getMesCnt() > lastMesCnt) {
            lastMesCnt = receiveMes.getMesCnt();
            return receiveMes;
        } else {
            return null;
        }

    }

    //发送消息
    public static void sendMessage(Message message, Socket clientSocket) throws IOException {
        message.setMesCnt(System.currentTimeMillis());
        ObjectOutputStream oos = new ObjectOutputStream(clientSocket.getOutputStream());
        oos.writeObject(message);
    }
}
